package com.huilunyingshi.videoprovider.serviceImp;

import com.alibaba.dubbo.config.annotation.Service;
import com.huilunyingshi.videoapi.service.TUserService;
import com.huilunyingshi.videoprovider.dao.TUserMapper;
import com.huilunyingshi.videoprovider.entity.TUser;
import com.hullunyingshi.until.ControllerResult;
import com.hullunyingshi.until.MD5Util;
import com.hullunyingshi.until.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class UserServiceImp implements TUserService {
    private final Logger logger = LoggerFactory.getLogger(AnimeServiceImp.class);


    @Autowired
    TUserMapper tUserMapper;

    /**
     * 注册用户
     * @param username
     * @param password
     * @param user_avatar
     * @param user_email
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult insertSelective(String username, String password, String user_avatar, String user_email) {


        try {
            if (StringTools.strHasNoData(username)) {
                return new ControllerResult("403", "注册账号不能为空", null);
            }

            if (StringTools.strHasNoData(password)) {
                return new ControllerResult("403", "注册密码不能为空", null);
            }
            if (StringTools.strHasNoData(user_email)) {
                return new ControllerResult("403", "注册邮箱不能为空", null);
            }
            if (StringTools.strHasNoData(user_avatar)) {
                user_avatar = "";
            }
            if (!StringTools.strHasNoData(tUserMapper.getUser(username))) {
                return new ControllerResult("403", "账户存在", null);
            }
            MD5Util md5Util = new MD5Util();
            password = md5Util.getMD5(password);
            TUser a = new TUser();

            a.setUsername(username);
            a.setPassword(password);
            a.setUserAvatar(user_avatar);
            a.setUserEmail(user_email);
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            df.format(date);
            Date create_by = date;
            String token = "";
            a.setCreateBy(create_by);
            a.setToken(token);

            return new ControllerResult("200", "创建用户成功", tUserMapper.insertSelective(a));


        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }


    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult recordUser(String username, String password) {
        try {


            if (StringTools.strHasNoData(username)) {
                return new ControllerResult("403", "账户为空", null);
            }

            if (StringTools.strHasNoData(password)) {
                return new ControllerResult("403", "密码为空", null);
            }
            password = MD5Util.getMD5(password);
            TUser tur = tUserMapper.recordUser(username, password);
            if (StringTools.strHasNoData(tur)) {
                return new ControllerResult("403", "账户或密码错误", null);
            }
            String Token = StringTools.getUUID();
            tUserMapper.updateByUuid(username, Token);


            return new ControllerResult("200", "登录成功", tUserMapper.recordUser(username, password));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }
    }

    /**
     * 退出登录
     * @param username
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult outUser(String username) {
        try {
            if (StringTools.strHasNoData(username)) {
                return new ControllerResult("403", "账户为空", null);
            }
            TUser a = tUserMapper.outUser(username);

            if (a.getToken() == "") {
                return new ControllerResult("403", "账户已登出", null);
            }

            String Token = "";
            tUserMapper.updateByUuid(username, Token);


            return new ControllerResult("200", "登出成功", tUserMapper.outUser(username));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());
        }
    }

    /**
     * 更改密码
     * @param username
     * @param oldpassword
     * @param newpassword
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ControllerResult updateBypassword(String username, String oldpassword, String newpassword) {
        try {


            oldpassword = MD5Util.getMD5(oldpassword);
            TUser a = tUserMapper.recordUser(username,oldpassword);


            if (StringTools.strHasNoData(a)) {
                return new ControllerResult("403", "账户不存在或密码错误", null);
            }

            newpassword = MD5Util.getMD5(newpassword);
            if (a.getPassword().equals(newpassword)) {
                return new ControllerResult("403", "新旧密相同", null);
            }
            tUserMapper.updateByPassword(username, oldpassword, newpassword);
            a.setToken("");
            tUserMapper.updateByPrimaryKeySelective(a);
            return new ControllerResult("200", "更改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.toString());

        }


    }
}
